/*
 * Copyright 2007 Brian Watkins
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package bw.util;

import java.math.*;

public class Location
{
    public String place = "";
    public String country = "";
    public String state = "";
    public String city = "";
    public String zip = "";
    public String street = "";
    public String coordinates = "";

    public Location ()
    {
        //nothing
    }

    public boolean isAddress ()
    {
        return ( place == null || place.length() == 0 );
    }

    public BigDecimal getLatitude ()
    {
        BigDecimal d = null;

        try 
        {
            String lat = coordinates.substring( 1, coordinates.indexOf( "," ) );
            d = new BigDecimal( lat );
        }
        catch ( Exception ex )
        {
            //Log.getInstance().write( "Proble parsing lat: " + coordinates );
            d = new BigDecimal( 0d );
        }

        return d;
    }

    public BigDecimal getLongitude ()
    {
        BigDecimal d = null;

        try 
        {
            String lon = coordinates.substring( coordinates.indexOf( "," ) + 1, 
                                                coordinates.lastIndexOf( "," ) );
            d = new BigDecimal( lon );
        }
        catch ( Exception ex )
        {
            //Log.getInstance().write( "Proble parsing lon: " + coordinates );
            d = new BigDecimal( 0d );
        }

        return d;
    }

    public String toString ()
    {
        StringBuffer buf = new StringBuffer();
        buf.append( "Place: " ).append( place ).append( "\n" );
        buf.append( "Street: " ).append( street ).append( "\n" );
        buf.append( "City: " ).append( city ).append( "\n" );
        buf.append( "State: " ).append( state ).append( "\n" );
        buf.append( "Zip: " ).append( zip ).append( "\n" );
        buf.append( "Country: " ).append( country ).append( "\n" );
        buf.append( "Coordinates: " ).append( coordinates ).append( "\n" );
        buf.append( "Latitude: " ).append( getLatitude() ).append( "\n" );
        buf.append( "Longitude: " ).append( getLongitude() );

        return buf.toString();
    }
}